package com.appspile.android.ANMP.applocksexy.application.fragment;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.appspile.android.ANMP.applocksexy.R;
import com.appspile.android.ANMP.applocksexy.application.controller.CollectAppInstalled;
import com.appspile.android.ANMP.applocksexy.application.fragment.adapter.ApplicationManagerAdapter;
import com.appspile.android.ANMP.applocksexy.application.object.ApplockPackageInfo;
import com.appspile.android.ANMP.applocksexy.commons.LogUtils;
import com.appspile.android.ANMP.applocksexy.global.GlobalApp;

import java.util.ArrayList;
import java.util.List;


public class ApplicationManagerFragment extends Fragment {

    private static final String TAG = "Application Manager Fragment";
    private ApplicationManagerAdapter adapter;
    private ListView lvAppLockAppList;
    private List<ApplockPackageInfo> data = new ArrayList<ApplockPackageInfo>();
    private View rootView;

    public static ApplicationManagerFragment newInstance(String param1, String param2) {
        ApplicationManagerFragment fragment = new ApplicationManagerFragment();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    public ApplicationManagerFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private void Init()
    {
       // if(rootView != null)
        {

            lvAppLockAppList = (ListView) rootView.findViewById(R.id.lvAppLockAppList);
            adapter = new ApplicationManagerAdapter(getActivity(), data);
            lvAppLockAppList.setAdapter(adapter);
        }

        new GetAllAppInstalled().execute();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_application_manager, container, false);
        Init();
        return rootView;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

    private class GetAllAppInstalled extends AsyncTask<Void, Void, List<ApplockPackageInfo>>
    {
        @Override
        protected List<ApplockPackageInfo> doInBackground(Void... params) {

          //  data.addAll(CollectAppInstalled.getInstance().getPackages());

            return CollectAppInstalled.getInstance().getPackages();
        }

        @Override
        protected void onPostExecute(List<ApplockPackageInfo> data) {
          LogUtils.d(TAG, "----------- get app finish ----------");



            adapter.replaceAllData(data);
//            adapter.notifyDataSetChanged();
        }
    }



}
